// pages/contact/list/index.js
import {contactModel} from '../model.js';
var area = require('../../../utils/area.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    country:"中国",
    province:"省份",
    city:"城市",
    countryArr:['中国','国外'],
    show:false,
    showType:'country',
    areaList: area.default,
    list:[],
    start:0,
    limit:15
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    contactModel.getAd('contact',(res)=>{
      this.setData(Object.assign(this.data,res));
    });
    
    this.getList(this.data.country,'','');
  },

  getList(country,province,city){
    if(province=="省份"||!province){
      province="";
    }
    if(city=='城市'||!city){
      city='';
    }

    let data={
      country,
      province,
      city,
      start:this.data.start
    };

    contactModel.getList(data,(res)=>{
      let list=this.data.list.concat(res.data.list);
      this.setData({
        list:list,
        start:list.length,
      });
    });
  },

  onLower(e){
    this.getList(this.data.country,this.data.province,this.data.city);
  },

  onCountry(e){
    this.setData({
      showType:'country',
      show:true
    });
    
  },

  onArea(){
    this.setData({
      showType:'area',
      show:true
    });
  },

  clickCountry(e){
    let value=e.detail.value;
    let areaList=this.data.areaList;
    if(value=='国外'){
      areaList={};
    }else{
      areaList=area.default;
    }
    this.setData({
      country:value=='全部'?"国家":value,
      areaList,
      show:false,
      list:[],
      start:0
    });
    this.getList(value,'','');
  },

  clickArea(e){
    let values=e.detail.values;
    this.setData({
      province:values[0].name,
      city:values[1].name,
      show:false,
      list:[],
      start:0
    });

    this.getList(this.data.country,values[0].name,values[1].name);
  },

  onCancel(){
    this.setData({
      show:false
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})